package com.tanhua.manage.controller;



import com.tanhua.manage.service.AdminService;
import com.tanhua.manage.vo.ErrorResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.zookeeper.OpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("system/users")
public class AdminController {
    @Autowired
    private AdminService adminService;

    /**
     * 用户登录
     * @param param
     * @return
     */
    @PostMapping("login")
    public ResponseEntity<Object> login (@RequestBody Map<String,String>param ){
        try {
            String username = param.get("username");//账号
            String password = param.get("password");//密码
            String verificationCode = param.get("verificationCode");//验证码
            String uuid = param.get("uuid");//同获取验证码时uuid

            String data = this.adminService.login("username", "password",
                    "verificationCode", "uuid");
            if(StringUtils.isNotEmpty(data)){
                //登录成功
                Map<String,Object> result=new HashMap<>(2);
                String[] ss = StringUtils.split(data, '|');

                result.put("token", ss[0]);
                result.put("isNew", Boolean.valueOf(ss[1]));
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("登录失败！").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

   /* @PostMapping("profile")
    public ResponseEntity<Object> saveAdmin(@RequestBody Map<String,String>param,
                                          @RequestHeader("Authorization") Object token) {

        this.adminService.save
    }*/
}
